package com.atguigu.tingshu.dispatch.job;

import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.dispatch.mapper.XxlJobLogMapper;
import com.atguigu.tingshu.model.dispatch.XxlJobLog;
import com.atguigu.tingshu.search.client.SearchFeignClient;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class DispatchJobHandler {
    @Autowired
    private SearchFeignClient searchFeignClient;

    @Autowired
    private XxlJobLogMapper xxlJobLogMapper;


    //  更新排行榜
    @XxlJob("rangking")
    public void rangking() {
        log.info("更新排行榜");
        XxlJobLog xxlJobLog = new XxlJobLog();
        xxlJobLog.setJobId(XxlJobHelper.getJobId());
        Result result = null;
        try {
            //  获取系统时间
            long startTime = System.currentTimeMillis();
            //  做远程调用执行功能！
            result = searchFeignClient.updateLatelyAlbumRanking();
            long endTime = System.currentTimeMillis();
            xxlJobLog.setTimes((int) (endTime - startTime));
            xxlJobLog.setStatus(1);
        } catch (Exception e) {
            xxlJobLog.setStatus(0);
            xxlJobLog.setError(e.getMessage());
            throw new RuntimeException(e);
        }
        xxlJobLogMapper.insert(xxlJobLog);
        log.info("更新排行榜结果：{}", result.getData());
    }

}